
// 混入 (mixin) 提供了一种非常灵活的方式，来分发 Vue 组件中的可复用功能
// methods  data  computed  watch mounted created  一个混入对象可以包含任意组件选项


import Vue from 'vue'
import { mapState, mapMutations, mapActions } from 'vuex'
import { baseURL } from './../api/request';
import { reg } from './../utils/validate';
import { ShowLoading, ShowSuccess, ShowError, ShowToast } from './../utils/message';
import pic from '@/assets/images/Pikachu.jpg'
import { tochangeuserinfo } from '@/api/api'
import { tagList } from '@/utils'
import _ from 'lodash'
// 全局混入
Vue.mixin({
  data() {
    return {
      base: 'https://xxx.www.com',
      num: 3000,
      baseURL,
      reg,
      pic,
      tagList,
      loda: _,
    }
  },
  computed: {
    ...mapState([
      'userInfo',
      'hbanners',
      'myms'
    ])
  },
  watch: {

  },
  methods: {
    throttle(func, interval) {
      let sign = true;
      return function () {
        // 在函数开头判断标志是否为 true，不为 true 则中断函数
        if (!sign) return;
        sign = false;
        setTimeout(() => {
          func.apply(this, arguments)
          sign = true;
        }, interval)
      }
    },
    // 防抖
    debounce(func, interval) {
      let timer = null;
      return function () {
        timer && clearTimeout(timer)
        // 重新调用setTimeout
        timer = setTimeout(() => {
          func.apply(this, arguments)
          timer = null;
        }, interval)
      }
    },
    toten(value) {
      return value < 10 ? '0' + value : value;
    },
    formatDate(date) {
      return `${date.getFullYear()}-${this.toten(date.getMonth() + 1)}-${this.toten(date.getDate())}`;
    },
    async changeavatar(path) {
      let res = await tochangeuserinfo({
        avatar: path
      })
      if (res.code == 200) {
        this.getUesrInfoData({
          ...this.userInfo,
          avatar: path
        })
      }
    },
    ShowLoading,
    ShowSuccess,
    ShowError,
    ShowToast,
    ...mapActions([
      'getUserInfoAsync',
      'getHomeBannerAsync',
      'getMyMsListAsync'
    ]),
    ...mapMutations([
      'changeCount',
      'changeMsg',
      'getUesrInfoData',
      'setHomeBanners'
    ]),
    gotopage(url) {
      this.$router.push(url)
    },
    numAdd() {
      this.num++;
    }
  },
  mounted() {
    // console.log('这是全局的混入的生命周期 mounted ')
  }
})
